GBML manual / Lists

list_insert

Puts value into target at index (0 is the first place), and everything from there on shifts one place towards the end. Returns true if value went in, false if index was out of range (target is left unchanged). If target is already full, the last item falls off the end and is lost - on purpose, with no error.

Syntax

list_insert(target, index, value) -> bool

Arguments

NameType
targetlist
indexint
valueelement

Returns

bool

Description

Puts value into target, a list, at the place index (0 is the first place), and moves everything from that place onward one step towards the end to make room. Returns true if value went in, and false if index was not a place it could go - target is left exactly as it was.

index can be any place that is already in the list, or the place just after the last item, which is list_length(target). Putting a value there is the same as list_add. A number below 0, or bigger than the length, is a false. list_remove counts places the same way, so list_remove(items, 2) takes out whatever list_insert(items, 2, x) put in.

The items that moved keep the order they had. Nothing is shuffled.

When the list is full, the last item is lost

If the list is already full, the item at the very end falls off and is gone. The new value goes in; the one that no longer fits is lost. There is no error, no warning and no crash.

This is on purpose, and it is worth knowing before it happens. A list is a box of a fixed size, and the Game Boy Advance has no spare memory to make the box bigger while the game runs. When there is no room, something has to give, and list_insert always gives up whatever would end up past the end.

If losing the last item would hurt, look first:

if (list_length(items) < 6) {      // 6 is the size this list was made with
    list_insert(items, 0, "key");
}

list_add follows the same rule from the other side. On a full list it keeps everything that is already there and drops the new item (it tells you with false). list_insert keeps the new item and drops the last one. In both, the thing that would end up past the end is the thing that is lost.

One corner: inserting at the very end of a full list (index equal to the length) has no last item to push off - the new value would be the one past the end. So it is the new value that is dropped, nothing changes, and you get false.

What it does not do

There is no list_get or list_set to go with it, because you do not need them. Read and change an item that is already in the list with []: items[2] and items[2] = 5 (see Lists). list_insert is for the one thing [] cannot do, which is to put a new item in the middle.

Example

/// @type {list<string>}
/// @capacity 4
var bag = ["rope", "map"];
list_insert(bag, 1, "torch");        // true - bag is now rope, torch, map
list_insert(bag, 0, "key");          // true - bag is now key, rope, torch, map, and is full
list_insert(bag, 0, "gem");          // true - gem goes first, and map falls off the end
var no_place = list_insert(bag, 9, "coin"); // false - there is no place 9, nothing changed

On the GBA

The same order-preserving shift the vector's insert does, O(n) in how many items come AFTER index. index may be anything from 0 to list_length(target) - list_length(target) itself means "after the last item", where list_add puts one. A fixed-size vector has no room for an N+1th item and the Game Boy Advance has no memory to grow it into, so when target is full this drops the LAST item first (the vector's pop_back) to make the room - the one rule "whatever would end up past the end is lost". Inserting at the very end of a full list has no last item to drop but the new value itself, so nothing changes and it returns false. It never asserts: an out-of-range index or a full list are ordinary answers, not crashes. The value is worked out before anything moves, so list_insert(items, 0, items[list_length(items) - 1]) copies the last item to the front even on a full list.

See also: list_addlist_removelist_lengthListslist_createData containers